
//
// 120519 - AH - (Aura) Created.
//

package org.soekrat.server.orbsuppliers.s2;

import java.util.List;

import org.soekrat.AttributeObject;
import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.pit.Content;
import org.soekrat.engine.pit.Node;
import org.soekrat.engine.pit.Pit;
import org.soekrat.environment.Orb;
import org.soekrat.environment.OrbSupplier;
import org.soekrat.server.serialization.ChatOutputSerializable;
import org.soekrat.server.serialization.ConversationElementSerializable;
import org.soekrat.server.serialization.RetrievedSerializable;
import org.soekrat.server.serialization.SoekratChatResponseSerializable;
import org.soekrat.server.serialization.VisitorAccountSerializable;

/**
 * <p>
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class Soekrat2ChatOutputFactory extends S2OrbSupplier 
  implements ChatOutputFactory 
{
  public Soekrat2ChatOutputFactory(){} 
  
  // Specified in S2OrbInitializer.
  public String supply(Orb orb, List<OrbSupplier> secondPass)
  {
    orb.setAttribute("chatoutput-factory", this);
    return null;
  }

  /**
   * <p>
   *   Process....
   * </p>
   * @param orb
   * @param visitor
   * @param ceId
   * @return
   */
  public ChatOutputSerializable process(Orb orb, String visitor, long ceId)
  {
    if (orb==null)
    {
      throw new NullPointerException("orb");
    }
    if (visitor==null)
    {
      throw new NullPointerException("visitor");
    }
    
    Soekrat2Engine engine=(Soekrat2Engine)orb.getAttribute("soekrat-engine");
    if (engine==null)
    {
      throw new IllegalArgumentException(
        "Soekrat2ChatOutputFactory: Orb is missing engine");
    }
     
    Pit pit=engine.getPit();
    
    @SuppressWarnings("unchecked")
    List<AttributeObject> painted=
      (List<AttributeObject>)orb.getAttribute(Orb.PAINT);
    
    //String stamp=orb.getStamp();
    
    ChatOutputSerializable output=new ChatOutputSerializable();
    
    for (AttributeObject curr : painted)
    {
      Object l=curr.getAttribute("semantic-effect");
      String semanticeffect=(l==null) ? null : l.toString();
      
      String name=curr.getName();
      
      System.out.println("Try find links for name="+name+
        ", result of semantic-effect="+semanticeffect);
      
      RetrievedSerializable r=new RetrievedSerializable(name);
      output.addRetrieved(r);
      
      String config=config(name);
      r.config(config);
      
      if (name!=null)
      {
        if (semanticeffect!=null)
        {
          Node node=pit.getNode(name);
          Content[] content=node.getContent(semanticeffect);
          for (Content c : content) 
          {
            String txt=c.getText();
            if (!c.isNote()) 
            {
              r.link(txt);
            }
          }
        }
      }
    }
      
    //output.addConversationObject(cos);
      
    SoekratChatResponseSerializable sr=new SoekratChatResponseSerializable();
    sr.account(new VisitorAccountSerializable(visitor));
    sr.ce(new ConversationElementSerializable(ceId));
    sr.chatOutput(output);
    
    return output;
  }
  
  private String config(String name)
  {
    StringBuilder sb=new StringBuilder();
    int n=name.length();
    for (int i=0; i<n; i++)
    {
      char c=name.charAt(i);
      if (Character.isLetter(c)) sb.append(c);
      else                       sb.append("_");
    }
    
    return "./"+sb.toString()+".config";
  }
  
}
